﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text;
using System;

public class PlayerManager : MonoBehaviour
{
    public static PlayerManager instance;
    // 基础数据

    // 玩家最高得分
    public int playerBestScore ;
    // 玩家金币
    public int money;
    // 玩家火箭长度
    public int rocketLength;
    // 音量大小
    public float volume;
    // 当前头部皮肤ID
    public  int rocketID;
    // 当前玩家持有皮肤数量
    public int hasHeadCount;
    // 玩家皮肤是否持有的数组，标注每个皮肤当前玩家是否持有
    public bool[] rocketHeadList;
    
    private void Awake()
    {
        instance = this;
        LoadGame();
    }


    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void LoadGame()
    {
        if (PlayerPrefs.HasKey("Money"))
        {
            money = PlayerPrefs.GetInt("Money");
        }
        else
        {
            money = 3000;
        }
        if (PlayerPrefs.HasKey("PlayerBestScore"))
        {
            playerBestScore = PlayerPrefs.GetInt("PlayerBestScore");
        }
        else
        {
            playerBestScore = 0;
        }
        if (PlayerPrefs.HasKey("Sound"))
        {
            volume = PlayerPrefs.GetInt("Sound");
        }
        else
        {
            volume = 1;
        }
        if (PlayerPrefs.HasKey("RocketHeadList"))
        {
            rocketHeadList = GetBoolArray("RocketHeadList");
        }
        else
        {
            rocketHeadList = new bool[15]
            {
                true,false,false,false,false,
                true,false,false,false,false,
                true,false,false,false,false
            };
        }

        if (PlayerPrefs.HasKey("RocketLength"))
        {
            rocketLength = PlayerPrefs.GetInt("RocketLength");
        }
        else
        {
            rocketLength = 5;
        }

        if (PlayerPrefs.HasKey("HasHeadCount"))
        {
            hasHeadCount = PlayerPrefs.GetInt("HasHeadCount");
        }
        else
        {
            hasHeadCount = 1;
        }

        if (PlayerPrefs.HasKey("RocketID"))
        {
            rocketID = PlayerPrefs.GetInt("RocketID");
        }
        else
        {
            rocketID = 0;
        }

    }

    // 存档bool数组
    public void SetBoolArray(string key, params bool[] boolArray)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < boolArray.Length - 1; i++)
        {
            sb.Append(boolArray[i]).Append("|");
        }
        sb.Append(boolArray[boolArray.Length - 1]);
        PlayerPrefs.SetString(key, sb.ToString());
    }

    private bool[] GetBoolArray(string key)
    {
        string[] stringArray = PlayerPrefs.GetString(key).Split('|');
        bool[] boolArray = new bool[stringArray.Length];
        for (int i = 0;i < stringArray.Length; i++)
        {
            boolArray[i] =Convert.ToBoolean(stringArray[i]);
        }
        return boolArray;
    }
}
